package pri.ydy.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import pri.ydy.msmservice.service.MsmService;

import java.util.Map;

/**
 * @author YeDongYong
 * @date 2020/12/17 - 9:14
 */
@Service
public class MsmServiceImpl implements MsmService {

    @Override
    public boolean sendMsm(Map<String, Object> param, String phone) {

        if(StringUtils.isEmpty(phone)) return false;

        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAI4G9J3BMTxoyjk1zqEk1y", "Oo6BtcLcTaybVfi4q5RJFZlEJeEFJA");
        IAcsClient client = new DefaultAcsClient(profile);

        //设置相关参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2020-12-25");
        request.setAction("SendSms");

        //设置发送的相关参数
        request.putQueryParameter("PhoneNumbers", phone);//发送的手机号
        request.putQueryParameter("SignName", "AAAAA在校教育网站");// 申请阿里云 签名名称
        request.putQueryParameter("TemplateCode", "SMS_207160053"); //申请阿里云， 模板code
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//Map集合中的验证码转换为JSON

        //进行最终发送
        /*try {
            CommonResponse response=client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }*/
        //TODO 发送短信
        return true;
    }
}
